package ltd.dujiabao.dex.json;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author : DuJiabao
 * @Project : common-delay-executor
 * @Time : 2024/3/23 14:30
 * @Desc :
 */
public class JsonUtils {
    public static final ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper();
    }

    public static <T> T json2Bean(byte[] bytes, Class<T> clazz) {
        try {
            return objectMapper.readValue(bytes, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T json2Bean(String json, Class<T> clazz) {
        return json2Bean(json.getBytes(), clazz);
    }

    public static <T> T object2Bean(Object object, Class<T> clazz) {
        return json2Bean(bean2Json(object), clazz);
    }

    public static String bean2Json(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
